function viewOrders(params)
{
    if(params.today)
    {
        window.location.href = "/order/view/?type=today";
    }
    else if(params.all)
    {
        window.location.href = "/order/view/?type=all";
    }
}

function showMask()
{
    height = parseFloat($(document.documentElement).attr("clientHeight"));

    if(height < $("html").height())
    {
        height = $("html").height();
    }


    $("#mask").css("left", "0").css("top", "0").width($("html").width()).height(height).show();
}

function showDialog(obj)
{
    showMask();
    var row = $(obj).parents("tr:first").find("td");

    var dialogRow = $("#tclist").find("tr");

     $("#hdnOrderId").val($(row[0]).text());
     $(dialogRow[0]).find("td:last").text($(row[1]).text());
     $(dialogRow[1]).find("td:last").text($(row[2]).text());
     $(dialogRow[2]).find("td:last").text($(row[3]).text());
     $(dialogRow[3]).find("td:last").text($(row[4]).text() + " * " + $(row[5]).text() + " = " + parseFloat($(row[4]).text())* parseFloat($(row[5]).text()));
     $(dialogRow[4]).find("td:last").text($(row[7]).text());
     $(dialogRow[5]).find("td:last").text($(row[8]).text());
     $(dialogRow[6]).find("td:last").text($(row[9]).text());
     
     $("#tctitle").text($(row[1]).text() + "的定餐记录");

     $("#tc").css("left", parseInt($(document).width()/2 - $("#tc").width()/2).toString() + "px");
     $("#tc").css("top", parseInt($(document.documentElement).attr("clientHeight") / 2 - $("#tc").height() / 2 + $(document.documentElement).attr("scrollTop")).toString() + "px");
     $("#tc").css("z-index", "100");
     $("#tc").show();
}

function closeDialog()
{
    $("#tc").hide();
    $("#mask").hide();
}

function pay()
{
    var payURL = "/order/pay/?orderId=" + $("#hdnOrderId").val() + "&type=" + $("#hdnType").val();
    if($('#hdnType').val() == 'people')
    {
        $.get(payURL, {}, function(){
                changePeople(document.getElementById("peopleId"));
                closeDialog();
        });
    }
    else
    {
        window.location.href = payURL;
    }
}

function del()
{
    var delURL = "/order/delete/?orderId=" + $("#hdnOrderId").val() + "&type=" + $("#hdnType").val();
    if($('#hdnType').val() == 'people')
    {
        $.get(delURL, {}, function(){
                changePeople(document.getElementById("peopleId"));
                closeDialog();
        });
    }
    else
    {
        window.location.href = delURL;
    }
}

function changeFoods(obj)
{
    var shopId = $(obj).val();

    $("#note").text($("#shopId").find("option:selected:first").attr("note"));

    var foodObj = $("#foodId");
    foodObj.empty();
    foodObj.append("<option>Loading...</option>");
    foodObj.attr("disabled", "disabled");
    $.getJSON('/order/getfoodsbyshop/', {'shopId': shopId, 'rnd': Math.random()}, function(data,b)
    {
        foodObj.empty();
        for(var i = 0; i < data.length; i++)
        {
            foodObj.append("<option value='" + data[i].id + "'>" + data[i].name + " - " + data[i].price + "</option>")
        }

        foodObj.removeAttr("disabled");
    });
}

function changePeople(obj)
{
    $("#ajaxLoader").show();
    obj = $(obj);

    $("#orderlisttitle").text(obj.find("option:selected:first").text() + "的未付款记录");
    var peopleId = obj.val();

    var unpaidList = $("#orderlist tbody");

    $.getJSON('/order/getOrders/', {'type': 'unpaid', 'peopleId': peopleId, 'rnd': Math.random()}, function(data,b)
    {
        unpaidList.empty();
        var totalPrice = fillOrderList(unpaidList, data);

        $("#orderlist tfoot tr td").text("合计: " + totalPrice);

        $("#ajaxLoader").hide();
    });
}

function getOrders()
{

    $("#orderlisttitle").text("订餐历史");
    var unpaidList = $("#orderlist tbody");

    $.getJSON('/order/getOrders/', {'type': 'all', 'rnd': Math.random()}, function(data,b)
    {
        unpaidList.empty();
        var totalPrice = fillOrderList(unpaidList, data);

        $("#orderlist tfoot tr td").text("合计: " + totalPrice);
    });
}

function fillOrderList(orderList, data)
{
    var totalPrice = 0;

    for(var i = 0; i < data.length; i++)
    {
        var subTotal = parseFloat(data[i].Food.price) * parseInt(data[i].Order.count);
        totalPrice += subTotal;
        rowHTML = "<tr>";

        rowHTML += "<td style='display:none'>" + data[i].Order.id + "</td>";
        rowHTML += "<td style='display:none'>" + data[i].People.name + "</td>";
        rowHTML += "<td>" + data[i].Shop.name + "</td>";
        rowHTML += "<td>" + data[i].Food.name + "</td>";
        rowHTML += "<td style='display:none'>" + data[i].Food.price + "</td>";
        rowHTML += "<td>" + data[i].Order.count + "</td>";
        rowHTML += "<td>" + subTotal + "</td>";
        rowHTML += "<td>" + $.PHPDate("Y-m-d", new Date(data[i].Order.created.replace(/-/g, "/"))) + "</td>";
        rowHTML += "<td style='display:none'>" + (data[i].Order.paid == 0 ? "N" : "Y") + "</td>";
        rowHTML += "<td style='display:none'>" + $.PHPDate("Y-m-d", new Date(data[i].Order.modified.replace(/-/g, "/"))) + "</td>";
        rowHTML += "<td><a href='#' onclick='showDialog(this); return false;'>详细</a></td>";

        rowHTML += "</tr>";

        orderList.append(rowHTML);
    }

    return totalPrice;
}

$(function()
{
    $("#note").text($("#shopId").find("option:selected:first").attr("note"));
    changePeople($("#peopleId")[0]);
    $("#btnOrder").hover(function()
        {
            $(this).attr("src", "/img/order_btn_active.png");
        }, function()
        {
            $(this).attr("src", "/img/order_btn.png");
        });
    $("#btnClose").hover(function()
        {
            $(this).attr("src", "/img/close_btn_active.png");
        }, function()
        {
            $(this).attr("src", "/img/close_btn.png");
        });

});
